﻿// json_2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <string>
#include <fstream>
#include "cJSON.h"
using namespace std;


//从文件读json数据到string
void Read_cJSON(const char* path, string& data) {
    ifstream fr(path);
    if (!fr) {
        cout << "can`t open file!" << endl;
        return;
    }
    char ch;
    while ((ch = fr.get()) != EOF) {
        data.push_back(ch);
    }
    fr.close();
}

//解析json字符串
void AnalyzeJson(cJSON* root) {
    for (int j = 0; j < cJSON_GetArraySize(root); j++) {
        cJSON* item = cJSON_GetArrayItem(root, j);
        if (item->type == cJSON_Object) {//数组
            if (!cJSON_IsNull(item)) {
                if (item->string)//可能存在对象名为NULL{"key":"value"}对象名为NULL   )
                    cout << "cJSON_Object " << j << " :" << item->string << "    cJSON_GetArraySize(Object): " << cJSON_GetArraySize(item) << endl;
                AnalyzeJson(item);
            }
        }
        else if (item->type == cJSON_Array) {
            if (!cJSON_IsNull(item)) {//对象
                if (item->string)
                    cout << "cJSON_Array " << j << " :" << item->string << "    cJSON_GetArraySize(Array): " << cJSON_GetArraySize(item) << endl;
                AnalyzeJson(item);
            }
        }
        else {
            if (!cJSON_IsNull(item)) {//键值对，注意键类型
                if (item->type == cJSON_String)
                    cout << "key(string) " << j << " : " << item->string << ";   value:" << item->valuestring << endl;
                if (item->type == cJSON_Number)
                    cout << "key(number) " << j << " : " << item->string << ";   value:" << item->valueint << endl;
            }
        }
    }
    cout << endl << endl;
}


int main()
{
    string data;
    Read_cJSON("./sipconfig.json", data);
    cJSON* root = cJSON_Parse(data.c_str());
    cout << "all ObjectSize():  " << cJSON_GetArraySize(root) << endl;
    AnalyzeJson(root);
    cJSON_Delete(root);
}




/*
///测试数据
{
  "cJSON_Object1": {
    "cJSON_Object1-1": {
      "key1": "valuestring",
      "key2": 0,
      "key3": "{0.000000,0.000000,0.000000,0.000000,0.000000}",
      "key4": "0.000000",
      "key5": "0.000000",
      "key6": "0.000000",
      "key7": "0.000000",
      "key8": "18.542000",
      "key9": "23.088600",
      "key10": "27.094180",
      "key11": "27.094180",
      "key12": 0,
      "key13": 24
    },
    "cJSON_Object1-2": {
      "key1": "valuestring",
      "key2": 0,
      "key3": "{0.000000,0.000000,0.000000,0.000000,0.000000}",
      "key4": "0.000000",
      "key5": "0.000000",
      "key6": "0.000000",
      "key7": "0.000000",
      "key8": "18.542000",
      "key9": "23.088600",
      "key10": "27.094180",
      "key11": "27.094180",
      "key12": 0,
      "key13": 24
    },
    "key14": 1,
    "key15": 2,
    "key16": 3
  },
  "cJSON_Object2": [
    {
      "Shape_num": 1,
      "Shape_List0": {
        "iShapeType": 24,
        "iIsPositive": 0,
        "dcx": 0,
        "dcy": 0,
        "diameter": 0,
        "dx1": 71.075042,
        "dy1": 66.265044,
        "dx2": 71.399908,
        "dy2": 65.940178,
        "dwidth": 0.1016,
        "TVYArcPoint": "{0.000000,0.000000,0.000000,0.000000,0.000000}",
        "Polygon_DARCP_num": 0,
        "Polygon_DARCP_List": {
        }
      }
    },
    {
      "Shape_num": 1,
      "Shape_List1": {
        "iShapeType": 24,
        "iIsPositive": 0,
        "dcx": 0,
        "dcy": 0,
        "diameter": 0,
        "dx1": 71.075042,
        "dy1": 66.265044,
        "dx2": 71.399908,
        "dy2": 65.940178,
        "dwidth": 0.1016,
        "TVYArcPoint": "{0.000000,0.000000,0.000000,0.000000,0.000000}",
        "Polygon_DARCP_num": 0,
        "Polygon_DARCP_List": {
        }
      }
    },
    {
      "Shape_num": 2,
      "Shape_List1": {
        "iShapeType": 24,
        "iIsPositive": 0,
        "dcx": 0,
        "dcy": 0,
        "diameter": 0,
        "dx1": 71.075042,
        "dy1": 66.265044,
        "dx2": 71.399908,
        "dy2": 65.940178,
        "dwidth": 0.1016,
        "TVYArcPoint": "{0.000000,0.000000,0.000000,0.000000,0.000000}",
        "Polygon_DARCP_num": 0,
        "Polygon_DARCP_List": {
        }
      },
      "Shape_List2": {
        "iShapeType": 24,
        "iIsPositive": 0,
        "dcx": 0,
        "dcy": 0,
        "diameter": 0,
        "dx1": 71.075042,
        "dy1": 66.265044,
        "dx2": 71.399908,
        "dy2": 65.940178,
        "dwidth": 0.1016,
        "TVYArcPoint": "{0.000000,0.000000,0.000000,0.000000,0.000000}",
        "Polygon_DARCP_num": 0,
        "Polygon_DARCP_List": {
        }
      }
    }
  ]
}
*/








